package com.jtoon.servlet;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jtoon.bean.Comics;
import com.jtoon.bean.User;
import com.jtoon.util.PMF;

public class Login extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		long cid = request.getParameter("cid") == null ? 0 : Long.valueOf(request.getParameter("cid"));
		long eid = request.getParameter("eid") == null ? 0 : Long.valueOf(request.getParameter("eid"));
		int eOrder = request.getParameter("eOrder") == null ? 0 : Integer.valueOf(request.getParameter("eOrder"));
		int imgNum = request.getParameter("imgNum") == null ? 0 : Integer.valueOf(request.getParameter("imgNum"));
		String mode = request.getParameter("mode") == null ? "" : request.getParameter("mode");
		
		RequestDispatcher rd = request.getRequestDispatcher("/page/login.jsp"); 
		request.setAttribute("cid", cid);
		request.setAttribute("eid", eid);
		request.setAttribute("eOrder", eOrder);
		request.setAttribute("imgNum", imgNum);
		request.setAttribute("mode", mode);
		rd.forward(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		boolean error = false;
		String msg = "";
		String id = request.getParameter("id");
		String pw = request.getParameter("pw");
		long cid = request.getParameter("cid") == null ? 0 : Long.valueOf(request.getParameter("cid"));
		long eid = request.getParameter("eid") == null ? 0 : Long.valueOf(request.getParameter("eid"));
		int eOrder = request.getParameter("eOrder") == null ? 0 : Integer.valueOf(request.getParameter("eOrder"));
		int imgNum = request.getParameter("imgNum") == null ? 0 : Integer.valueOf(request.getParameter("imgNum"));
		String mode = request.getParameter("mode") == null ? "" : request.getParameter("mode");
		User user = null;
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(User.class);
		
		try{ 
			query.setFilter("uid == uidParam");
			query.declareParameters("String uidParam");
			List<User> userList = (List<User>)query.execute(id);
			
			if(id == null){
				error = true;
				msg = "아이디를 입력하세요.";
			}else if(pw == null){
				error = true;
				msg = "비밀번호를 입력하세요.";
			}else if(userList.size() < 1){
				error = true;
				msg = "아이디가 존재하지 않습니다.";
			}else{
				user = userList.get(0);
				if(!user.getPwd().equals(pw)){
					error = true;
					msg = "비밀번호가 일치하지 않습니다.";
				}
			}
			
			if(!error){
				HttpSession session = request.getSession();
				session.setAttribute("id", user.getUid());
				
				if(mode.equals("add")){
					response.sendRedirect("/bookmark?cid=" + cid + "&eid=" + eid + "&eOrder=" + eOrder + "&imgNum=" + imgNum + "&mode=" + mode);
				}else{
					response.sendRedirect("/index");
				}  
				
			}else{
				RequestDispatcher rd = request.getRequestDispatcher("/page/login.jsp"); 
				request.setAttribute("msg", msg);
				request.setAttribute("cid", cid);
				request.setAttribute("eid", eid);
				request.setAttribute("eOrder", eOrder);
				request.setAttribute("imgNum", imgNum);
				request.setAttribute("mode", mode);
				rd.forward(request, response);  
			}   
			
		}finally{
			pm.close(); 
		} 
	}
	
}
